﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ToWaTimeReportMVC4.Models;

namespace ToWaTimeReportMVC4.Controllers
{
    [Authorize]
    public class AdminController : Controller
    {

        ToWaService service = new ToWaService();
        ToWaTimeReportMVC4Entities db = new ToWaTimeReportMVC4Entities();

        //
        // GET: /Admin/
        
        public ActionResult Index()
        {
            return View(service.GetAdminIndexList());
        }

        //
        // GET: /Admin/CreateUser

        public ActionResult CreateUser()
        {
            return View();
        } 

        //
        // POST: /Admin/CreateUser

        [HttpPost]
        public ActionResult CreateUser(UserWrapper userWrapper)
        {
            try
            {
                // TODO: Add insert logic here
                service.AddUser(userWrapper.Username, userWrapper.Password, userWrapper.Password, userWrapper.Email, service.ConvertTypeToInt(userWrapper.Type), userWrapper.Salary, 0);

                return RedirectToAction("Index", "Admin");
            }
            catch
            {
                return RedirectToAction("Index", "Home");
            }
        }
        
        //
        // GET: /Admin/DeleteUser/5
 
        public ActionResult DeleteUser(int id)
        {
            service.RemoveUserById(id);

            return RedirectToAction("Index");
        }

        //
        // GET: /Admin/CreateProject

        public ActionResult CreateProject()
        {
            List<User> customer = service.GetAllCustomers();
            List<SelectListItem> select = new List<SelectListItem>();
            foreach (User u in customer)
            {
                select.Add(new SelectListItem
                {
                    Text = u.Username,
                    Value = "" + u.Id
                });
            }
            return View(new ProjectWrapper { CustomerNames = select });
        }

        //
        // POST: /Admin/CreateProject

        [HttpPost]
        public ActionResult CreateProject(ProjectWrapper pWrapper)
        {
            try
            {
                service.AddProject(pWrapper.Name, int.Parse(pWrapper.SelectedValue), pWrapper.Description);

                return RedirectToAction("Index", "Admin");
            }
            catch
            {
                return RedirectToAction("Index", "Home");
            }
        }

        //
        // GET: /Admin/Delete

        public ActionResult DeleteProject(int id)
        {
            service.RemoveProjectById(id);

            return RedirectToAction("Index");
        }

        public ActionResult SearchRapport()
        {
            return View(new SearchRapport { Consultants = service.GetConsultantDropDown(), normal = 40 });
        }

        public ActionResult DisplaySearchResult(int consultId, string startDate, string endDate, int normal)
        {
            List<Rapport> rapports = new List<Rapport>();
            if (consultId < 0)
            {
                List<User> consultants = service.GetAllConsultant();
                foreach (User u in consultants)
                {
                    rapports.Add(service.GetRapportForConsultant(u.Id, startDate, endDate, normal));
                }
            }
            else
            {
                rapports.Add(service.GetRapportForConsultant(consultId, startDate, endDate, normal));
            }
            string[] temp = startDate.Split(' ');
            ViewBag.StartDate = startDate;
            temp = endDate.Split(' ');
            ViewBag.EndTime = endDate;
            ViewBag.Normal = normal;
            return View(rapports);
        }

        public ActionResult DisplaySingelReport(int consultId, string startDate, string endDate, int normal)
        {
            Rapport r = service.GetRapportForConsultant(consultId, startDate, endDate, normal);
            ViewBag.Consultant = r.ConsultantName;
            ViewBag.TotalHours = r.total;
            ViewBag.Overtime = r.overtime;
            ViewBag.NormalTime = normal;
            return View(r.ProjectJobs);
        }
    }
}
